package com.solome.vertx;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class ConverterRegistry {
	private static final Map<Class<?>, Function<String, Object>> converters = new HashMap<>();

	static {
		converters.put(Integer.class, Integer::parseInt);
		converters.put(int.class, Integer::parseInt);
		converters.put(Long.class, Long::parseLong);
		converters.put(long.class, Long::parseLong);
		converters.put(Double.class, Double::parseDouble);
		converters.put(double.class, Double::parseDouble);
		converters.put(Float.class, Float::parseFloat);
		converters.put(float.class, Float::parseFloat);
		converters.put(Byte.class, Byte::parseByte);
		converters.put(byte.class, Byte::parseByte);
		converters.put(Short.class, Short::parseShort);
		converters.put(short.class, Short::parseShort);
		converters.put(Boolean.class, Boolean::parseBoolean);
		converters.put(boolean.class, Boolean::parseBoolean);
		converters.put(BigDecimal.class, BigDecimal::new);
		converters.put(String.class, s -> s);
	}

	public static void registerConverter(Class<?> type, Function<String, Object> converter) {
		converters.put(type, converter);
	}

	public static Function<String, Object> getConverter(Class<?> type) {
		return converters.get(type);
	}
}
